﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MiniCrawler.Entity;

namespace MiniCrawler.DataAccess.Repositories
{
    public class DownloadLinkHelper
    {
        public static DownloadLink GetLink(string url)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks where s.Value.Equals(url) select s;
            if (link != null && link.Count() > 0)
            {
                return link.First();
            }
            else
            {
                return null;
            }
        }


        public static List<DownloadLink> GetUndownloaded(string hostName)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks where( s.IsDownloaded == false && s.Website == hostName) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return null;
            }
        }

        public static List<DownloadLink> GetDownloaded(string hostName)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks where (s.IsDownloaded == true && s.Website == hostName) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return null;
            }
        }


        public static DownloadLink GetLinkByFile(string fileName)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks where s.Value.Contains(fileName) select s;
            if (link != null && link.Count() > 0)
            {
                return link.First();
            }
            else
            {
                return null;
            }
        }

        public static DownloadLink GetLinkByFile(CrwlDataEntities dbContext, string fileName)
        {
            var link = from s in dbContext.DownloadLinks where s.Value.Contains(fileName) select s;
            if (link != null && link.Count() > 0)
            {
                return link.First();
            }
            else
            {
                return null;
            }
        }

        public static DownloadLink GetLink(CrwlDataEntities dbContext, string url)
        {
            var link = from s in dbContext.DownloadLinks where s.Value.Equals(url) select s;
            if (link != null && link.Count() > 0)
            {
                return link.First();
            }
            else
            {
                return null;
            }
        }

        public static DownloadLink GetLink(DownloadLink link)
        {
            if (link != null)
            {
                return GetLink(link.Value);
            }
            else
            {
                return null;
            }
        }

        public static bool IsLinkExist(string url)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks where s.Value.Equals(url) select s;
            if (link != null && link.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsLinkDownloaded(string url)
        {
            var link = GetLink(url);
            if (link != null)
            {
                return link.IsDownloaded;
            }
            return false;
        }

        public static List<DownloadLink> SelectUncheckedLinks()
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks where (s.IsDownloaded == false) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DownloadLink>();
            }
        }
        public static DownloadLink SelectFirstUncheckedLinks()
        {
            var db = new CrwlDataEntities();
            var link = (from s in db.DownloadLinks where ((s.State == 1 || s.State == null)) select s).First();

            return link;

        }

        public static List<DownloadLink> SelectLinks()
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DownloadLink>();
            }
        }

        public static List<DownloadLink> SelectLinks(string webSite)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks where s.Website == webSite select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DownloadLink>();
            }
        }

        public static bool HasUncheckedLinks()
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DownloadLinks where ((s.State == 1 || s.State == null)) select s;
            if (link != null && link.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int CreateLink(DownloadLink link)
        {
            var db = new CrwlDataEntities();
            db.DownloadLinks.Add(link);
            return db.SaveChanges();
        }

        public static int CreateLink(string value, string website, DateTime dateCreated, bool isDownloaded)
        {
            var link = new DownloadLink()
            {
                DateCreated = dateCreated,
                IsDownloaded = isDownloaded,
                Value = value,
                Website = website
            };
            return CreateLink(link);
        }

        public static int UpdateLink(DownloadLink link)
        {
            var db = new CrwlDataEntities();
            var currentLink = GetLink(db, link.Value);
            currentLink.IsDownloaded = link.IsDownloaded;
            currentLink.State = link.State;
            currentLink.Path = link.Path;
            db.Entry(currentLink).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public static int UpdateState(DownloadLink link, int state)
        {
            var db = new CrwlDataEntities();
            var currentLink = GetLink(db, link.Value);
            currentLink.State = state;
            db.Entry(currentLink).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public static int UpdateLink(string url)
        {
            var db = new CrwlDataEntities();
            var currentLink = GetLink(db, url);
            currentLink.IsDownloaded = true;
            db.Entry(currentLink).State = EntityState.Modified;
            return db.SaveChanges();
        }


        public static int FixPathUrl(string hostName)
        {
                 
            var db = new CrwlDataEntities();
            var links = from s in db.DownloadLinks 
                        where (
                            s.Website == hostName && 
                            s.Path.Contains("|") ||
                            s.Path.Contains("?") || s.Path.Contains("*") || s.Path.Contains("\"") || s.Path.Contains(":") || s.Path.Contains("/"))
                        select s;

            var m = links.ToList();

            if (m != null && m.Count() > 0)
            {
                foreach (var downloadLink in links)
                {
                    string s = downloadLink.Path;
                    string old = s;

                    downloadLink.Path =
                       s.Replace("/", "-")
                        .Replace("|", "-")
                        .Replace("?", string.Empty)
                        .Replace("*", " ")
                        .Replace("\"", string.Empty)
                        .Replace(":", "-");

                    var z = UpdateLink(downloadLink);
                    var n = 0;

                }
            }

            return 1;

        }

    }
}
